package com.iot.websocket;

import com.alibaba.fastjson.JSON;

/**
 * websocket消息封装
 */
public class WebSocketMessage {

    // 业务类型：subscribe 添加订阅, unsubscribe 取消订阅, publish 发布消息, common 普通消息
    private String bizCode;
    /**
     * 订阅主题
     */
    private String topic;
    // 消息内容
    private Object data;

    private static WebSocketMessage getInstance(String bizCode, String topic, Object data) {
        WebSocketMessage webSocketMessage = new WebSocketMessage();
        webSocketMessage.bizCode = bizCode;
        webSocketMessage.topic = topic;
        webSocketMessage.data = data;
        return webSocketMessage;
    }

    public static String message(String bizCode){
        return JSON.toJSONString(WebSocketMessage.getInstance(bizCode, null, null));
    }

    public static String message(String bizCode, Object msg){
        return JSON.toJSONString(WebSocketMessage.getInstance(bizCode, null, msg));
    }

    public String getBizCode() {
        return bizCode;
    }

    public void setBizCode(String bizCode) {
        this.bizCode = bizCode;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String dataString() {
        return data == null ? "" : data.toString();
    }

    /*public JSONObject getDataJSONObject() {
        return data == null ? null : JSONObject.parseObject(data.toString());
    }*/
}
